package com.seven.springboot.controller.yang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seven.springboot.domain.yang.AccountOutput;
import com.seven.springboot.enums.StatusCode;
import com.seven.springboot.enums.ValidStatus;
import com.seven.springboot.pojo.Account;
import com.seven.springboot.service.yang.AccountService;
import com.seven.springboot.util.MD5Utils;
import com.seven.springboot.util.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

//@RestController
@Controller
@RequestMapping("yang/account")
@Api("Account相关操作")
public class AccountController {

	private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

	@Autowired
	private AccountService accountService;

    @ApiOperation(value = "根据用户id获取用户信息",notes="根据用户id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "form",dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/getAccountById",method = RequestMethod.POST)
    public Account getAccountById(@RequestParam(value = "id") int id) {
    	if(id <= 0) {
    		logger.error("id invalid,id:{}",id);
    	}
        return accountService.getAccountById(id);
    }
    
    @RequestMapping(value = "/login")
    public ModelAndView toLogin(ModelAndView mv) {
    	mv.setViewName("yang/login");
    	return mv;
    }
    
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public AccountOutput login(@RequestParam(value = "accountNo") String accountNo,@RequestParam(value = "password") String password) {
    	AccountOutput output = new AccountOutput();
    	if(StringUtils.isEmpty(accountNo) || StringUtils.isEmpty(password)) {
    		return output.setCode(StatusCode.MISSING_PARAM.getIndex()).setMsg(StatusCode.MISSING_PARAM.getMessage());
    	}
    	Account account = accountService.getAccountByQuery(accountNo, ValidStatus.VALID);
    	if(account == null) {
    		return output.setCode(StatusCode.USER_NOT_EXIST.getIndex()).setMsg(StatusCode.USER_NOT_EXIST.getMessage());
    	}
    	if(!account.getPassword().equalsIgnoreCase(password)) {
    		return output.setCode(StatusCode.ACCOUNT_OR_PASSWORD_ERROR.getIndex()).setMsg(StatusCode.ACCOUNT_OR_PASSWORD_ERROR.getMessage());
    	}
    	output.setData(account);
    	return output.setCode(StatusCode.SUCCESS.getIndex()).setMsg(StatusCode.SUCCESS.getMessage());
    }
    
    @RequestMapping(value = "/greeting")
    public ModelAndView greeting(ModelAndView mv) {
    	mv.setViewName("greeting");
    	mv.addObject("title","欢迎使用thymeleaf");
    	return mv;
    }
    
    public static void main(String[] args) {
		String str = MD5Utils.getMD5ofStr("123456");
		System.out.println(str);
	}
	
}
